- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy path79. Word Search.go
45 lines (41 loc) · 961 Bytes
/
79. Word Search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package leetcode
vardir= [][]int{
{-1, 0},
{0, 1},
{1, 0},
{0, -1},
}
funcexist(board [][]byte, wordstring) bool {
visited:=make([][]bool, len(board))
fori:=0; i<len(visited); i++ {
visited[i] =make([]bool, len(board[0]))
}
fori, v:=rangeboard {
forj:=rangev {
ifsearchWord(board, visited, word, 0, i, j) {
returntrue
}
}
}
returnfalse
}
funcisInBoard(board [][]byte, x, yint) bool {
returnx>=0&&x<len(board) &&y>=0&&y<len(board[0])
}
funcsearchWord(board [][]byte, visited [][]bool, wordstring, index, x, yint) bool {
ifindex==len(word)-1 {
returnboard[x][y] ==word[index]
}
ifboard[x][y] ==word[index] {
visited[x][y] =true
fori:=0; i<4; i++ {
nx:=x+dir[i][0]
ny:=y+dir[i][1]
ifisInBoard(board, nx, ny) &&!visited[nx][ny] &&searchWord(board, visited, word, index+1, nx, ny) {
returntrue
}
}
visited[x][y] =false
}
returnfalse
}